package com.frkj.yuan.activity;

import android.app.Dialog;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.frkj.yuan.R;
import com.frkj.yuan.api.bean.BaseInforBean;
import com.frkj.yuan.api.bean.VersionBean;
import com.frkj.yuan.api.category.LoginOutCategory;
import com.frkj.yuan.api.category.VersionCategory;
import com.frkj.yuan.api.presenter.LoginOutPresenter;
import com.frkj.yuan.api.presenter.VersionMsgPresenter;
import com.frkj.yuan.base.BaseActivity;
import com.frkj.yuan.dialog.OnQuiteDialogClickListener;
import com.frkj.yuan.dialog.RoundCornerDialog;
import com.frkj.yuan.main.ConstantStaticUrl;
import com.frkj.yuan.utils.CleanDataUtils;
import com.frkj.yuan.utils.FullScreenUtils;
import com.frkj.yuan.utils.SPUtils;
import com.frkj.yuan.utils.ToastUtil;

import butterknife.BindView;
import butterknife.OnClick;
import cn.ycbjie.ycstatusbarlib.bar.YCAppBar;

/**
 * 信息说明: 用户设置界面
 * company: 兰州丰荣信息科技有限公司
 * 创建时间: on 2022/3/5 16 ：27
 * 项目名称：yuanban
 */
public class UserSettingActivity extends BaseActivity implements LoginOutCategory.View {
    @BindView(R.id.base_title)
    TextView mTitleMsg;
    @BindView(R.id.clear_size)
    TextView mClearSize;
    @BindView(R.id.banben)
    TextView mBanben;
    private LoginOutPresenter mPresenter;
    private RoundCornerDialog dialog;
    @Override
    public int getLayoutId() {
        return R.layout.activity_user_setting;
    }

    @Override
    protected void onResume() {
        super.onResume();
        initLayoutView();
    }

    @Override
    public void initLayoutView() {
        String oldVersionName = null;
        try {
            oldVersionName = getPackageManager().getPackageInfo(getPackageName(), 0).versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        mBanben.setText("v"+oldVersionName);
        FullScreenUtils.getInstance().fullScreen(UserSettingActivity.this, true);
        mTitleMsg.setText("更多设置");
        mPresenter = new LoginOutPresenter(this);
        SetPersenter(mPresenter);
        String totalCacheSize = null;
        try {
            totalCacheSize = CleanDataUtils.getTotalCacheSize(UserSettingActivity.this);
            mClearSize.setText(totalCacheSize);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @OnClick({R.id.base_back, R.id.login_out, R.id.clear_cash, R.id.about_us, R.id.xieyi, R.id.yinsi})
    public void OnClick(View view) {
        Bundle bundle = new Bundle();
        switch (view.getId()) {
            case R.id.base_back:
                finishWitchAnimation();
                break;
            case R.id.login_out:
                dialog = new RoundCornerDialog(UserSettingActivity.this, "确认要退出登录吗", "确认", "取消", new OnQuiteDialogClickListener() {
                    @Override
                    public void onConfirm(Dialog dialog) {
                        mPresenter.getLoginOut();
                    }

                    @Override
                    public void onCancel(Dialog dialog) {
                        dialog.dismiss();
                    }
                });
                dialog.show();
                break;
            case R.id.clear_cash:
                try {
                    String totalCacheSize = CleanDataUtils.getTotalCacheSize(UserSettingActivity.this);
                    CleanDataUtils.clearAllCache(UserSettingActivity.this);
                    mClearSize.setText(totalCacheSize);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case R.id.yinsi:
                bundle.putString("xieyi", "4");
                openActivityWitchAnimation(ProtocolActivity.class, bundle);
                break;
            case R.id.xieyi:
                bundle.putString("xieyi", "2");
                openActivityWitchAnimation(ProtocolActivity.class, bundle);
                break;
            case R.id.about_us:
                openActivityWitchAnimation(AboutUsActivity.class);
                break;
        }
    }
    @Override
    public void getLoginOut(BaseInforBean bean) {
        if (bean.getCode() == ConstantStaticUrl.SUCCESS_CODE) {
            SPUtils.clear(UserSettingActivity.this);
            SharedPreferences sharedPreferences = getSharedPreferences("First", MODE_PRIVATE);
            //获取Editor对象的引用
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.clear();
            // 提交数据
            editor.commit();
            openActivityWitchAnimation(UserLoginActivity.class);
            finish();
            ToastUtil.showToastSUCCESS(UserSettingActivity.this, "成功退出登录");
        } else {
            ToastUtil.showToastINFO(UserSettingActivity.this, bean.getMsg());
        }
        dialog.dismiss();
    }

    @Override
    public void SetPersenter(Object persenter) {
    }

    @Override
    public void OnShowErrorMsg(String mErrorMsg) {
        ToastUtil.showToastERROR(UserSettingActivity.this, mErrorMsg);
    }


}
